package scenic.toolkit;

import java.awt.geom.Rectangle2D;

import scenic.Color;
import scenic.Graphics;
import scenic.SceneContainer;
import scenic.toolkit.TextSetter.TextObject;

public class TextElementView extends ElementView {
	TextElementModel model;
	
	public TextElementView(TextElementModel model) {
		this.model = model;
	}

	public void paint(Graphics g, TextSetter textSetter) {
		TextStyle style = model.getStyle();
		String text = model.getText().toString();
		
		g.setFont(style.getFont().getAwtFont());
		
		textSetter.beginElement(this);
		for(int i = 0; i < text.length(); i++) {
			Rectangle2D bounds = g.getTextLogicalBounds(text.substring(i, i + 1));
			textSetter.addObject(-bounds.getY(), bounds.getMaxY(), 
					bounds.getWidth(), Character.isSpaceChar(text.charAt(i)));
			
		}
	}
	
	public void drawObjects(SceneContainer scene, double x, double y, 
			TextSetter.TextObject objects[], int objectOffset, int offset, int length) {
		Graphics g = new Graphics(scene);
		float positions[] = new float[length * 2];
		float xp = 0;
		
		for(int i = 0; i < length; i++) {
			positions[i * 2] = xp;
			positions[i * 2 + 1] = 0;
			xp += objects[i + objectOffset].width;
		}
		
		g.setFont(model.getStyle().getFont().getAwtFont());
		g.translate(x, y);
		g.setFillColor(new Color(0.0f));
		g.drawText(model.getText().toString().substring(offset, offset + length), positions);
	}
}
